package com.wkbb.basic.thread;

import com.wkbb.basic.feign.DoorFeignService;
import com.wkbb.basic.model.Student;
import com.wkbb.basic.service.StudentService;
import com.wkbb.common.enums.ChangeClassOperateTypeEnum;
import com.wkbb.common.utils.ThreadPoolUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;

/**
 * 换班操作异步处理
 * */
@Slf4j
public class ChangeClassTask implements Runnable {

    private Integer operateType;
    private StudentService studentService;
    private DoorFeignService doorFeignService;
    private Long orgId;
    private Long gradeId;
    private Long classId;
    private List<Long> userList;
    private String operateUser;

    public ChangeClassTask(Integer operateType, StudentService studentService, DoorFeignService doorFeignService, Long orgId, Long gradeId, Long classId, List<Long> userList, String operateUser) {
        this.operateType = operateType;
        this.studentService = studentService;
        this.doorFeignService = doorFeignService;
        this.orgId = orgId;
        this.gradeId = gradeId;
        this.classId = classId;
        this.userList = userList;
        this.operateUser = operateUser;
    }

    @Override
    public void run() {
        try {
            log.info("进入换班操作异步处理开始.....start，operateType:{} orgId:{}，gradeId:{} classId:{}", operateType, orgId, gradeId, classId);
            Thread.sleep(1000);
            if(operateType == ChangeClassOperateTypeEnum.CHANGE_CLASS_TO_STUDENT_PERSON.getCode().intValue())
            {
                log.info("******ChangeClassTask 个体调班 begin****");
                for (Long userId : userList) {
                    ThreadPoolUtil.addPoolTask(new ChangeClassStudentTask(operateType,studentService,orgId,null,classId,userId,operateUser));
                }
                log.info("******ChangeClassTask 个体调班 end****");
            }
            else if(operateType == ChangeClassOperateTypeEnum.CHANGE_CLASS_TO_STUDENT_GRADUATE.getCode().intValue())
            {
                log.info("******ChangeClassTask 毕业 begin****");

                log.info("******ChangeClassTask 毕业 end****");
            }
            else if(operateType == ChangeClassOperateTypeEnum.CHANGE_CLASS_TO_STUDENT_UNIVERSITY.getCode().intValue())
            {
                log.info("******ChangeClassTask 肄业 begin****");

                log.info("******ChangeClassTask 肄业 end****");
            }
            else if(operateType == ChangeClassOperateTypeEnum.CHANGE_CLASS_TO_UP_CLASS.getCode().intValue())
            {
                log.info("******ChangeClassTask 升班 begin****");

                //更换班级下学生
                HashMap<String, Object> studentParams = new HashMap<>();
                studentParams.put("orgId", orgId);
                studentParams.put("classId", classId);
                List<Student> studentInfoList = studentService.getListByMapParams(studentParams);
                for(Student student : studentInfoList)
                {
                    ThreadPoolUtil.addPoolTask(new ChangeClassStudentTask(operateType,studentService,orgId,gradeId,null,student.getUserId(),operateUser));
                }

                //同步门禁等
                ThreadPoolUtil.addPoolTask(new ChangeClassSyncDataTask(ChangeClassOperateTypeEnum.CHANGE_CLASS_TO_UP_CLASS.getCode(),doorFeignService,orgId,null,null,null,null,null,operateUser));

                log.info("******ChangeClassTask 升班 end****");
            }

            log.info("进入换班操作异步处理结束.....");
        } catch (Exception e) {
            log.error("换班操作异步处理，{}", e);
            e.printStackTrace();
        }
    }

}
